@echo off


set CUR_DIR=%CD%
cd ..
set PARENT_DIR=%CD%
call set PROJECT=%%CUR_DIR:%PARENT_DIR%\=%%
echo project: %PROJECT%
cd %CUR_DIR%


if "%TAG_VER%" == "" set TAG_VER=1.0
set BUILD_V=%BUILD_NUMBER%
if NOT DEFINED BUILD_NUMBER set BUILD_V=1
set VERSION=%TAG_VER%.%BUILD_V%
echo version: %VERSION%


if exist package-lock.json (
echo install node modules ...
cmd /c npm install
)


echo typescript build ...
cmd /c tsc  
if not exist js\index.js exit /b 1
 
echo build ...
cmd /c webpack --mode production --target node --entry ./js/index.js --output-filename index.js || exit /b 1 


if exist output rd /s /q output
md output

echo write header ...
node -e fs.writeFileSync('dist/head','#!/usr/bin/env\u0020node\n')

echo pack output ...

if not exist resource (

copy /b dist\head+dist\index.js output\%PROJECT%
echo @node %%~dp0\%PROJECT% %%* > output\%PROJECT%.bat
if not exist output\%PROJECT% exit /b 1

) else (

md output\files
copy /b dist\head+dist\index.js output\files\%PROJECT%
echo @node %%~dp0\%PROJECT% %%* > output\files\%PROJECT%.bat
if not exist output\files\%PROJECT% exit /b 1
xcopy /y /s resource output\files
if exist output\files\app.info echo VERSION=%VERSION%>> output\files\app.info
where zip >nul 2>&1 || goto eof
cd output\files
zip -r ../%PROJECT%_%VERSION%.zip ./* 
cd ..\..
if not exist output\%PROJECT%_%VERSION%.zip exit /b 1
)

:eof
echo OK



